<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('日志列表')"/>
</head>
<style>
    table td{
        overflow:visible;/* 内容超出宽度时隐藏超出部分的内容 */
    }
</style>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form action="" class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">表名</label>
                <div class="layui-col-sm3 layui-col-md3 layui-col-lg3">
                    <select id="collectionNames" class="layui-col-sm3 layui-col-md3 layui-col-lg3" name="collectionName" lay-filter="collectionNames" lay-verify="required" lay-search="">
                    </select>
                </div>
                <label class="layui-form-label">对象类型</label>
                <div class="layui-input-inline">
                    <select id="objType" name="objType" lay-filter="objType" lay-search="">
                        <option value="">全部</option>
                        <option value="table">TABLE</option>
                        <option value="table">ROW</option>
                    </select>
                </div>
                <label class="layui-form-label">SQL类型</label>
                <div class="layui-input-inline">
                    <select id="type" name="type" lay-filter="type" lay-search="">
                        <option value="">全部</option>
                        <option value="insert">INSERT</option>
                        <option value="delete">DELETE</option>
                        <option value="update">UPDATE</option>
                        <option value="alert">ALERT</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">SQL主键ID</label>
                <div class="layui-input-inline">
                    <input id="id" name="id" class="layui-input" type="text" placeholder="SQL主键ID"/>
                </div>
                <label class="layui-form-label">SQL内容</label>
                <div class="layui-col-sm5 layui-col-md5 layui-col-lg5">
                    <input id="sql" name="sql" class="layui-input" type="text" placeholder="sql 内容 模糊搜索"/>
                </div>
                <label class="layui-form-label">&nbsp;</label>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-filter="query" lay-submit>
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button class="pear-btn pear-btn-md" type="reset">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="table" lay-filter="table"></table>
    </div>
</div>
<script id="bar" type="text/html">
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="detail"></i>
        详情
    </button>
</script>
</body>
<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery', 'popup'], function () {
        let table = layui.table;
        let form = layui.form;
        let MODULE_PATH = "/sqlLog/";
        let cols = [
            [
                {field: 'id', hide: true, title: 'id', align: 'center'},
                {field: 'rowIdValue', title: '主键id', width: 300},
                {field: 'dataMapOfJson', title: '数据'},
                {field: 'type', sort: true, title: 'SQL类型', width: 150},
                {field: 'objType', sort: true, title: '对象类型', width: 150},
                {field: 'createDate', sort: true, title: '创建时间', width: 180},
                {toolbar: '#bar', title: '操作', width: 120}
            ]
        ];

        table.render({
            elem: '#table',
            url: MODULE_PATH + 'data',
            page: true,
            cols: cols,
            skin: 'line',
            height: 'full-148',
            toolbar: '#toolbar',
            text: {none: '暂无记录信息'},
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
                title: "刷新"
            }, 'filter']
        });

        table.on('tool(table)', function (obj) {
            if (obj.event === 'detail') {
                window.detail(obj);
            }
        });
        // 获取所有集合名
        $.ajax({
            type: "get",
            url: '/logDatabase/getAllByQueryName',
            dataType: 'json',
            async: true,
            success: function (data) {
                if (data.success !== undefined && data.success) {
                    var collectionNames = data.data;
                    var optionNames = "<option value=\"\">直接选择或搜索选择</option>";
                    for (let i = 0; i < collectionNames.length; i++) {
                        var collectionName = collectionNames[i];
                        optionNames = optionNames + "<option value=\"" + collectionName + "\">" + collectionName + "</option>"
                    }
                    //清空数据
                    $("#collectionNames").empty("");
                    $('#collectionNames').append(optionNames);
                    form.render('select');
                }
            }
        });

        form.on('submit(query)', function (data) {
            window.refresh(data.field);
            return false;
        });

        window.detail = function (obj) {
            layer.open({
                type: 2,
                title: '详情',
                shade: 0.1,
                area: ['950px', '800px'],
                content: MODULE_PATH + 'detail?id=' + obj.data['id'] + '&collectionName=' + obj.data['collectionName']
            });
        }

        window.refresh = function (param) {
            table.reload('table', {where: param});
        }
        })
</script>
</html>